﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//



using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using KretschIT.WP_Fx.EasyMoq.Generator.Core;
using KretschIT.WP_Fx.EasyMoq.Generator.Items;

namespace KretschIT.WP_Fx.EasyMoq.Generator.Arguments
{
    internal class ParameterArgumentBuilder : StringBuilder, IParameterArgumentBuilder
    {
        private const string ParameterWithTypeFormat = "{0} {1}";

        private readonly INamingFactory nameFactory;

        public ParameterArgumentBuilder(INamingFactory nameFactory)
        {
            this.nameFactory = nameFactory;
        }

        public void Add(ParameterInfo parameterArgument, bool includeType)
        {
            if (parameterArgument == null)
            {
                return;
            }
            var parameter = this.CreateParameter(parameterArgument, includeType);
            this.Add(parameter);
        }

        private string CreateParameter(ParameterInfo parameterArgument, bool includeType)
        {
            if (includeType)
            {
                var type = parameterArgument.ParameterType;
                var typeName = this.nameFactory.GetNormalizedNameOf(type);
                if (type.IsGenericType)
                {
                    typeName += GenericsHelper.BuildGenerics(type.GetGenericArguments(), this.nameFactory);
                }
                return string.Format(ParameterWithTypeFormat, typeName, parameterArgument.Name);
            }
            return parameterArgument.Name;
        }
    }
}
